/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package com.crmapi.controller;


import com.crmapi.mapper.DeptMapper;
import com.crmapi.mapper.UserMapper;
import com.crmapi.pojo.Dept;
import com.crmapi.pojo.Result;
import com.crmapi.service.DeptService;
import com.crmapi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>Project: CRMSystem - DeptController</p>
 * <p>Powered by yzc On 2023-11-16 19:54:49</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Slf4j
@Api(tags = "部门管理")
@RestController
@RequestMapping("/api_v1.0")
public class DeptController extends BaseController {
    @Autowired
    DeptService deptService;
    //查询所有部门
    @GetMapping("/dept/list")
    public Result queryDept() {
        List<Dept> deptList = deptService.queryDept();
        return Result.success(deptList);
    }
    //按部门级别一级一级显示部门
    @GetMapping("/dept/tree")
    public Result queryTree() {
        List<Dept> deptList = deptService.queryTree();
        return Result.success(deptList);
    }
    //根据部门id来查询部门
    @GetMapping("/dept/byId")
    public Result queryById(String id){
        Dept dept = deptService.queryById(id);
        return Result.success(dept);
    }

    //查询子部门
    @GetMapping("/dept/child")
    public Result queryChile(String parentId){
        List<Map> map = deptService.queryChild(parentId);
        return  Result.success(map);
    }

    //查询一级部门
    @GetMapping("/dept/one")
    public  Result queryOne(){
        List<Dept> deptList = deptService.queryOne();
        return  Result.success(deptList);
    }

    //添加部门
    @PostMapping("/dept/insert")
    public Result insertDept(@RequestBody Dept dept){
        int rs = deptService.insert(dept);
        if (rs != 1)
            return Result.error("添加部门失败");
        return Result.success("添加部门成功");
    }

    //更新部门
    @PutMapping("/dept/update")
    public Result updateDept(@RequestBody Dept dept){
        int rs = deptService.update(dept);
        if (rs != 1)
            return Result.error("更新部门失败");
        return Result.success("更新部门成功");
    }

    //根据id删除部门
    @DeleteMapping("/dept/del")
    public Result del(String id){
        int rs = deptService.del(id);
        if (rs !=1 )
            return Result.error("部门删除失败");
        return Result.success("部门删除成功");
    }
}
